#ifndef __PROC_MONITOR_H__
#define __PROC_MONITOR_H__

#include<vector>
#include<string>
using namespace std;

#include<time.h>

#define HZ sysconf(_SC_CLK_TCK)

class ProcEnt
{
public:
  const pid_t pid;

  const string proc_dir;

  const string proc_statm;

  const string proc_stat;

  /* real uid */
  const uid_t ruid;

  /* effective uid, not used */
  const uid_t euid;

  /* saved uid, not used */
  const uid_t suid;

  /* file system uid, not used */
  const uid_t fuid;

  vector<string> cmd_vec;

  ProcEnt( const pid_t _pid, string & _proc_dir, pid_t ruid, pid_t euid, pid_t suid, pid_t fuid );
  ~ProcEnt();

  /* memory in kb */
  int mem_vir;
  int mem_res;
  int mem_shr;

  /* jiffies: user and kernel */
  int current_jiffy;
  long int jiffies_u[ 2 ];
  long int jiffies_k[ 2 ];
  inline void record_jiffy( long int ju, long int jk )
  {
    current_jiffy ^= 1;
    jiffies_u[ current_jiffy ] = ju;
    jiffies_k[ current_jiffy ] = jk;
  }

  struct timespec jiffy_times[ 2 ];
  inline void record_jiffy_time()
  {
    // must be called before record_jiffy
    clock_gettime( CLOCK_REALTIME, &jiffy_times[ current_jiffy ^ 1 ] );
  }

  inline void get_jiffy( long int * ju, long int * jk ) const 
  {
    *ju = jiffies_u[ current_jiffy ] - jiffies_u[ current_jiffy ^ 1 ];
    *jk = jiffies_k[ current_jiffy ] - jiffies_k[ current_jiffy ^ 1 ]; 
  }

  /* in millis */
  inline int get_duration() const
  {
    const struct timespec * b = &jiffy_times[ current_jiffy ];
    const struct timespec * a = &jiffy_times[ current_jiffy ^ 1 ];
    return (b->tv_sec - a->tv_sec) * 1000 + ( b->tv_nsec - a->tv_nsec ) / 1000000;
  }

  int born;

  int age;

  /* tmp variable */
  double _last_cpu_percentage;
  
};


#endif
